<!-- 当前会话界面 -->
<template>
  <div class="current-conversation">
    <el-container class="current-conversation-wapper" v-show="showCurrentConversation">
      <el-header class="conversation-header">
        <div class="conversation-header-name" :title="conversationName">
          {{ conversationName }}
        </div>
        <div class="conversation-close-button">
          <el-button
            @click="closeConversation"
            icon="el-icon-close"
            size="small"
            circle>
          </el-button>
        </div>
      </el-header>
      <el-container class="conversation-content">
        <el-container>
          <div 
            class="show-profile-button" :class="showProfile ? '' : 'hidden-profile-button'"
            @click="showProfileClick" 
            :title="showProfileTitle"
            v-show="showProfileButton">
          </div>
          <el-main class="conversation-message-content">
            <MessageItem v-for="message in currentMessageList" :key="message.ID" :message="message"/>
          </el-main>
          <el-footer 
            class="conversation-footer" 
            v-if="currentConversation.type !== TIM.TYPES.CONV_SYSTEM"
            height="120px">
            <MessageSendBox />
          </el-footer>
        </el-container>
        <el-aside 
          class="conversation-profile"
          v-show="showProfile"
          width="200px">
          <UserProfile v-if="currentConversation.type === TIM.TYPES.CONV_C2C" />
          <GroupProfile v-else-if="currentConversation.type === TIM.TYPES.CONV_GROUP" />
        </el-aside>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex';
import MessageSendBox from '../message/MessageSendBox.vue';
import MessageItem from '../message/MessageItem.vue';
import UserProfile from './conversationProfile/UserProfile.vue';
import GroupProfile from './conversationProfile/GroupProfile.vue';

export default {
  name: "current-conversation",
  data () {
    return {
      isShowProfile: true
    }
  },
  updated(){
    // 发送消息后自动滚动到最下，el-main获取不到refs.scrollHeight参数，通过以下方式解决
    let messageListNode = document.getElementsByClassName('conversation-message-content');
    messageListNode[0].scrollTop = messageListNode[0].scrollHeight;
  },
  watch: {
    currentUnreadCount(next) {
      if (!this.hidden && next > 0) {
        this.$store.dispatch('setMessageRead',this.currentConversation.conversationID);
      }
    },
    hidden(next) {
      if (!next && this.currentUnreadCount > 0) {
        this.$store.dispatch('setMessageRead',this.currentConversation.conversationID);
      }
    }
  },
  computed: {
    ...mapState({
      currentMessageList: state => state.conversation.currentMessageList,
      currentConversation: state => state.conversation.currentConversation,
      currentUnreadCount: state => state.conversation.currentConversation.unreadCount,
      currentMemberList: state => state.group.currentMemberList
    }),
    ...mapGetters(['hidden']),
    conversationName() {
      if (this.currentConversation.type === this.TIM.TYPES.CONV_C2C) {
        return this.currentConversation.userProfile.nick || this.currentConversation.userProfile.userID;
      }
      if (this.currentConversation.type === this.TIM.TYPES.CONV_GROUP) {
        return this.currentConversation.groupProfile.name || this.currentConversation.groupProfile.groupID;
      }
      if (this.currentConversation.type === this.TIM.TYPES.CONV_SYSTEM) {
        return '系统通知';
      }
      return '';
    },
    showCurrentConversation() {
      return !!this.currentConversation.conversationID;
    },
    showProfile(){
      if (this.currentConversation.type === this.TIM.TYPES.CONV_SYSTEM) {
        return false;
      }
      return this.isShowProfile;
    },
    showProfileTitle() {
      if(this.isShowProfile){
        return '隐藏会话资料';
      } else {
        return '显示会话资料';
      }
    },
    showProfileButton(){
      return this.currentConversation.type === this.TIM.TYPES.CONV_C2C || this.currentConversation.type === this.TIM.TYPES.CONV_GROUP;
    }
  },
  components: {
    MessageSendBox,
    MessageItem,
    UserProfile,
    GroupProfile
  },
  methods: {
    closeConversation(){
      this.$store.commit('resetCurrentConversation');
    },
    showProfileClick(){
      this.isShowProfile = !this.isShowProfile;
    }
  }
};
</script>

<style scoped>
.current-conversation {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  overflow: hidden;
}
.current-conversation-wapper {
  height: 100%;
  overflow: hidden;
}
.conversation-header {
  /* el-header默认高度60px */
  /* height: 60px;  */
  width: 100%;
  line-height: 60px;
  border-bottom: 1px solid #e7e7e7;
}
.conversation-header-name {
  float: left;
  font-size: 20px;
  max-width: 50%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.conversation-close-button {
  float: right;
}
.conversation-content{
  position: relative;
  overflow: hidden;
}
.conversation-footer {
  border-top: 1px solid #e7e7e7;
  overflow: hidden;
  padding: 0;
}
.conversation-profile {
  background-color: white;
  overflow: auto;
}
/* 这里right和conversation-profile的width有关，无法在style里设置 250px-15-1(border占1px)*/
.show-profile-button {
  position:absolute;
  cursor: pointer;
  right: 184px;
  z-index: 1;
  bottom: 50%;
  width: 15px;
  height: 30px;
}
.hidden-profile-button {
  right: 0;
  transform: rotate(180deg);
}
.show-profile-button.hidden-profile-button::before {
  background-color: white;
}
.show-profile-button::before {
  position: absolute;
  right: 0;
  z-index: 0;
  content: "";
  background-color: #f5f5f5;
  border: 1px solid #e7e7e7;
  border-radius: 0 100% 100% 0/50%;
  border-left: none;
  width: 15px;
  height: 30px;
}
.show-profile-button::after {
  position: absolute;
  top: 11px;
  right: 7px;
  content: "";
  width: 8px;
  height: 8px;
  border-top: 2px solid #a5b5c1;
  border-right: 2px solid #a5b5c1;
  transform: rotate(45deg);
}
</style>